<?php
namespace app\auth\model;

use think\Model;
use think\helper\Hash;

class User extends Model implements UserInterface
{

    protected static $pk            = 'id';
    protected static $authKey       = 'auth_key';
    protected static $loginField    = 'phone';
    protected static $passwordField = 'password';
    protected static $openId        = 'app_openid';


    /**
     * 用户 auth_key 更新写表
     * @Author  YanHao
     * @Date    2017-07-07
     * @version [version]
     * @param   [type]     $uid     [description]
     * @param   [type]     $authKey [description]
     * @return  [type]              [description]
     */
    public static function updateAuthKey($uid, $authKey)
    {  
        return self::update([self::$authKey=>$authKey], [self::$pk=>$uid]);
    }
    /**
     * 用户登录信息修改
     * @Author  YanHao
     * @Date    2017-07-07
     * @version [version]
     * @param   [type]     $uid        [description]
     * @param   array      $updateInfo [description]
     * @return  [type]                 [description]
     */
    public static function loginUpdate($uid, array $updateInfo)
    {
        return self::update($updateInfo, [self::$pk=>$uid]);
    }
    /**
     * 用户登录信息验证
     * @Author  YanHao
     * @Date    2017-07-07
     * @version [version]
     * @param   [type]     $loginField [description]
     * @param   [type]     $password   [description]
     * @return  [type]                 [description]
     */
    public static function validateLogin($loginField, $password = null)
    {
        $field = strlen($loginField) === 32 ? self::$openId : self::$loginField;
        $where = [$field=>$loginField];
        if (null !== $password) {
            $where[self::$passwordField] = Hask::make($password);
        }
        $find = self::field(self::$pk)->where($where)->find();
        return $find ? true : false;
    }
    /**
     * 验证用户身份
     * @Author  YanHao
     * @Date    2017-07-07
     * @version [version]
     * @param   [type]     $uid     用户ID
     * @param   [type]     $authKey authKey
     * @return  [type]              [description]
     */
    public function validateAuth($uid, $authKey)
    {
        $where = [
            self::$pk      => $uid,
            self::$authKey => $authKey
        ];
        $find = self::field(self::$pk)->where($where)->find();
        return $find ? true : false;
    }
}